package com.boilerCloud.dao.device;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.ModelCode;
import com.boilerCloud.dto.device.ModelInfo;
import com.boilerCloud.dto.device.ModelType;
import com.boilerCloud.util.StringUtil;
@Repository
public class ModeDao extends BaseDao {

	public void del(int id)throws Exception {
		String sql = "update d_model_info set is_delete ='y' where id = ?";
		this.jdbcTemplate.update(sql,id);
		
	}

	public void save(ModelInfo model)throws Exception {
		String sql = "insert into d_model_info(name,brand,produce_time,code_id,type_id,"
				+ "create_time,modify_time,is_delete,pic_url,info_type) "
				+ "values(?,?,?,?,?,now(),now(),'n',?,?)";
		List<Object> params = new ArrayList<Object>();
		params.add(model.getName());
		params.add(model.getBrand());
		params.add(model.getProduceTimeStr());
		params.add(model.getCodeId());
		params.add(model.getTypeId());
		params.add(model.getPicUrl());
		params.add(model.getInfoType());
		this.jdbcTemplate.update(sql,params.toArray());
		
	}

	public void edit(ModelInfo model)throws Exception {
		String sql = "update d_model_info set name=?,brand=?,produce_time=?,code_id=?,"
				+ "type_id=?,pic_url=?,info_type=?,modify_time=now() where id=?";
		List<Object> params = new ArrayList<Object>();
		params.add(model.getName());
		params.add(model.getBrand());
		params.add(model.getProduceTimeStr());
		params.add(model.getCodeId());
		params.add(model.getTypeId());
		params.add(model.getPicUrl());
		params.add(model.getInfoType());
		params.add(model.getId());
		this.jdbcTemplate.update(sql,params.toArray());
		
		
	}

	public void qryList(ModelInfo model, HzlqswPage<ModelInfo> page) throws Exception{
		String countsql = "select count(*) from d_model_info a "
				//+ " left join d_model_code b on b.id = a.code_id"
				//+ " left join d_model_type c on c.id = a.type_id"
				+ " where a.is_delete='n' ";
			
		String listSql = "select a.* "
					+ " from d_model_info a "
					//+ " left join d_model_code b on b.id = a.code_id"
					//+ " left join d_model_type c on c.id = a.type_id"
					+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(model.getName())){
			countsql += " and a.name like ?";
			listSql += " and a.name like ?";
			params.add("%" + model.getName() +"%");
		}
		if(!StringUtil.isEmpty(model.getBrand())){
			countsql += " and a.brand like ?";
			listSql += " and a.brand like ?";
			params.add("%" + model.getBrand() +"%");
		}
		if(model.getTypeId() != 0 && model.getTypeId() != -1){
			countsql += " and a.type_id = ?";
			listSql += " and a.type_id = ?";
			params.add(model.getTypeId());
		}		
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, ModelInfo.class);
		
	}

	public void qryModelTypeList(ModelType model, HzlqswPage<ModelType> page)throws Exception {
		String countsql = "select count(*) from d_model_type a "
				+ " where a.is_delete='n' ";
			
		String listSql = "select a.*,b.code "
					+ " from d_model_type a "
					+ " left join d_model_code b on b.id = a.code_id"
					+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(model.getName())){
			countsql += " and a.name like ?";
			listSql += " and a.name like ?";
			params.add("%" + model.getName() +"%");
		}
		if(model.getCodeId() != 0 && model.getCodeId() != -1){
			countsql += " and a.code_id = ?";
			listSql += " and a.code_id = ?";
			params.add(model.getCodeId());
		}		
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, ModelType.class);
		
	}

	public void editModelType(ModelType model)throws Exception {
		String sql = "update d_model_type set code_id =?,name=?, modify_time = now() where id = ?";
		this.jdbcTemplate.update(sql,model.getCodeId(),model.getName(),model.getId());
		
	}

	public void addModelType(ModelType model) throws Exception {
		String sql = "insert into d_model_type(code_id,name,create_time,modify_time,is_delete) values(?,?,now(),now(),'n')";
		this.jdbcTemplate.update(sql,model.getCodeId(),model.getName());
		
	}

	public void delModelType(int id) throws Exception{
		String sql = "update d_model_type set is_delete ='y' where id = ?";
		this.jdbcTemplate.update(sql,id);
		
	}

	public List<ModelType> getModelTypeList(ModelType object)throws Exception {
		String sql = "select * from d_model_type where is_delete='n' and code_id = ?";
		return this.queryForList(sql, ModelType.class,object.getCodeId());
	}

	public List<ModelInfo> getModelList(ModelInfo object)throws Exception {
		
		String sql = "select * from d_model_info where is_delete='n'";
		return this.queryForList(sql, ModelInfo.class);
	}

	public ModelInfo getModelInfo(int id)throws Exception {
		String sql = "select * from d_model_info where id=? limit 1";
		return this.queryForObject(sql,ModelInfo.class,id);
	}

	public ModelType getModelType(int id) throws Exception{
		String sql = "select * from d_model_type where id=? limit 1";
		return this.queryForObject(sql,ModelType.class,id);
	}

	/**
	 * <b>Description:<em><blockquote>
	 * 		
	 *
	 * </blockquote></em></b>
	 * 
	 * @Title: qryModelInfoByName
	 * @param model
	 * @return    
	 * @author Ran_cc
	 * @date 2018年5月24日 
	 */
	public ModelInfo qryModelInfoByName(String name) throws Exception {
		
		String sql = "select * from d_model_info where 1=1 and name = ? limit 1 ";
		
		return this.queryForObject(sql,ModelInfo.class,name);
	}

}
